package com.zookeeper.test.basic;

import java.io.IOException;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;


public class CreateSession { 

	private static ZooKeeper zookeeper;
	public static void main(String[] args) throws IOException, InterruptedException {
		zookeeper = new ZooKeeper("192.168.1.101:2181", 100, new SelfDefinedWatcher());
		System.out.println(zookeeper.getState());
		Thread.sleep(Integer.MAX_VALUE);
	}
	
	static class SelfDefinedWatcher implements Watcher {
		@Override
		public void process(WatchedEvent event) {
			System.out.println("监听事件：" + event);
			if (event.getState() == KeeperState.SyncConnected) {
				if (event.getType() == EventType.None && null == event.getPath()) {
					doSomething();
				}
			}
		}
		
		private void doSomething() {
			System.out.println("do something");
		}
	}
}
